#include<stdio.h>
#include<unistd.h>
#include<iostream>
#include<sys/socket.h>
#include<netinet/in.h>
#include<sys/epoll.h>
#include<arpa/inet.h>
#include<string.h>
#include<map>
class TcpSer{
  public:
  TcpSer(int port=2927){
  sockfd=-1;
  port_=port;
  }
  ~TcpSer(){
  
  }

  int InitTcp(){
    sockfd=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    if(sockfd<0){
      perror("socket");
      return -1;
    }
    return 0;
  }

  int Mbind(int port=2927){
  struct sockaddr_in addr;
  addr.sin_family=AF_INET;
  addr.sin_port=htons(2927);
  addr.sin_addr.s_addr=inet_addr("0.0.0.0");
  int ret=bind(sockfd,(struct sockaddr*)&addr,sizeof(addr));
  if(ret<0){
    perror("bind");
    return -1;
  }
  return ret;
  }

  int Mlisten(){
    return listen(sockfd,5);
  }
  int Maccept(struct sockaddr_in* addr=NULL,socklen_t* addrlen=NULL){ 
    int new_sockfd=accept(sockfd,(struct sockaddr*)addr,addrlen);
    if(new_sockfd<0){
      perror("accept");
      return -1;
    }
    return new_sockfd;
  }
  int Mrecv(std::string* msg){
    char buf[1024]={0};
   ssize_t r_size = recv(sockfd , buf , sizeof(buf)-1 , 0);
   if(r_size<0){
     perror("recv");
     return -1;
   }else if(r_size==0){
     return -100;
   }else{
     *msg+=buf;
   }
   return r_size;
  }

  int Getfd(){
    return sockfd;
  }
  int Msend(std::string& msg){
    return send(sockfd,msg.c_str(),msg.size(),0);
  }
  void sockset(int sockfd_){
   sockfd=sockfd_;
  }
  void Mclose(){
    close(sockfd);
  }
  private:
    int sockfd;
    int port_;

};
